let text = require('../../../../common/util/text.js')
const days = ['','','']
for (let i = 1; i <= 200; i++) {
  days.push(i)
}
days.push('')
days.push('')
days.push('')
const xAudio = wx.createInnerAudioContext({
  obeyMuteSwitch: false,

  success: function (res) {
      console.log(res)
      console.log("开启静音模式下播放音乐的功能");
  },

  fail: function (err) {
      console.log(err)
      console.log("静音设置失败");
  },
});
const xBackMusic = wx.getBackgroundAudioManager();
xAudio.src = 'https://7633-v3-92ah4-1300208657.tcb.qcloud.la/static/bell001.mp3?sign=49a3516df85ff1824d33bbe0089a12df&t=1663130591'
xBackMusic.src = 'https://7633-v3-92ah4-1300208657.tcb.qcloud.la/static/bell001.mp3?sign=49a3516df85ff1824d33bbe0089a12df&t=1663130591'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    week:'',
    treatment_item_id:'',
    action_id:'',
    info:{},
    VideoContext:'',
    num:'./image/3.svg',
    countdown:false,
    number:4,
    percent:0,
    percent_min:0,
    baseline:false,
    implement:false,
    btnlist:['身体受伤','疼痛难忍无法继续','其他原因'],
    current:-1,
    isplay:false,//false
    days,
    action_num:'',
    record_weight:0,
    previous:{},
    next:{},
    practice_feeling:'',//练习感受
    current_swiper:0,
    type:'',
    retroaction:'',
    name:'',
    controls:false,
    previewBtn:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that =this;
    that.setData({
      week:Number(options.week),
      treatment_item_id:options.treatment_item_id,
      action_id:options.action_id, 
      type:Number(options.type),
      name:options.name
    })
    that.xInit(Number(options.week),options.treatment_item_id,options.action_id)
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.videoContext = wx.createVideoContext('myVideo')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that =this;
    getApp().globalData.lock=false
    wx.setNavigationBarTitle({
      title: that.data.name,
    })
    that.videoContext = wx.createVideoContext('myVideo')
    
  },
  xChildReload: function () {
    var pages = getCurrentPages();
    if (pages.length > 1) {
      var beforePage = pages[pages.length - 3];
      beforePage.xInfo();
    }
  },
  xInit:function(week,treatment_item_id,action_id){
    let that =this;
    // 单个动作详情
    let _dataObj = {
      week: week,
      treatment_item_id: treatment_item_id,//任务_id
      action_id: action_id// 动作_id
    }
    wx.cloud.callFunction({
      name: 'digital_therapy',
      data: {
        action: "get_action_info",
        dataObj: _dataObj
      }
    }).then(res => {
      
      res.result.explain = text.text(res.result.explain)
            that.setData({
              info:res.result,
            })
      if(res.result.record.length>0){
        if(res.result.record[0].retroaction){
          for(let index in that.data.btnlist){
            if(that.data.btnlist[index]== res.result.record[0].retroaction){
              that.setData({
                retroaction:res.result.record[0].retroaction,
                current:index
              })
            }
          }
        }
        if(res.result.record[0].action_data.record_mode==1){
          if(res.result.record[0].exercise_duration>=1){
            that.setData({
              current_swiper:res.result.record[0].exercise_duration-1,
              // action_num:res.result.record[0].exercise_duration
            })
          }else{
            that.setData({
              current_swiper:res.result.record[0].exercise_duration,
              // action_num:res.result.record[0].exercise_duration
            })
          }
        }else{
          if(res.result.record[0].movements_number>=1){
            that.setData({
              current_swiper:res.result.record[0].movements_number-1,
              action_num:res.result.record[0].movements_number
            })
          }else{
            that.setData({
              current_swiper:res.result.record[0].movements_number,
              action_num:res.result.record[0].movements_number
            })
          }
          
        }
      }
    })
    // 动作列表
    let _dataObj2 = {
      week: week,
      treatment_item_id: treatment_item_id
    }
    wx.cloud.callFunction({
      name: 'digital_therapy',
      data: {
        action: "get_action_list",
        dataObj: _dataObj2
      }
    }).then(res => {
       var newarr=[];
       for(let i in res.result){
         if(res.result[i].is_complete_today==false){
            newarr.push(res.result[i])
         }
         if(res.result[i]._id==action_id && res.result[i].is_complete_today==true){
            newarr.push(res.result[i])
         }
       }
       for(let index in newarr){
         if(newarr[index]._id==action_id && index>=1){
           that.setData({
            previous:newarr[Number(index)-1]
           })
         }
         if(newarr[index]._id==action_id && index<newarr.length-1){
          that.setData({
           next:newarr[Number(index)+1]
          })
        }
       }
    })
  },
  previewPlay:function(){
    let that =this
    that.setData({
      controls:true,
      previewBtn:false
    })
    that.videoContext.play()
  },
  bindPlay: function() {
    let that =this;
    if(that.data.number==4 && !that.data.info.is_complete_today){
      that.setData({
        countdown:true,
        controls:true,
        previewBtn:false
      })
      var timer= setInterval(function () { 
        that.setData({
          num:'./image/'+(that.data.number-1)+'.svg',
          number:that.data.number-1
         })
        if(that.data.number==0){
          clearInterval(timer)
          that.setData({
            countdown:false,
            isplay:true
          })
          that.videoContext.play()
          that.videoContext.seek(0)
          var timers =setInterval(function(){
              that.setData({
                percent:that.data.percent+1
              })
              // if(that.data.percent<=60){
              //   that.setData({
              //     percent_min:that.data.percent+"'"+"'"
              //   })
              // }else{
              //   let n =(Number(that.data.percent)-60)
              //   that.setData({
              //     percent_min:"1'"+n +"'"+"'"
              //   })
              // }
              // if(that.data.percent==90){
                
              // }
          },1000)
        }
      }, 1000)
    }else{
      wx.showModal({
        title: "提示",
        content: "此项任务今天已完成，请明天继续",
        showCancel: false,
        confirmText:'好的'
      })
    }
  },
  bindended:function(){
    let that=this;
    // if(that.data.isplay==true){
      xAudio.title = '1'
      xAudio.singer = '1'
      xAudio.coverImgUrl = '1'
      xAudio.play()
    // }
  },
  bindPause: function() {
    this.videoContext.pause()
  },
  // 查看基线
  toseebaseline:function(){
    this.setData({
      baseline:true
    })
  },
  // 无法练习
  xTounimplement:function(){
    this.setData({
      isplay:!this.data.isplay,
      implement: !this.data.implement
    })
  },
  // 练习感受
  input:function(e){
    this.setData({
      practice_feeling:e.detail.value
    })
  },
  // 选择原因
  xTochoose:function(e){
    this.setData({
      current:e.currentTarget.dataset.index,
      retroaction:e.currentTarget.dataset.reason
    })
  },
  // 动作数量
  bindscroll(e) {
    console.log(e)
    // this.setData({
    //   action_num: e.detail.value[0]+1
    // })
  },
  // 动作数量
  // bindchange(e) {
  //   this.setData({
  //     action_num: e.detail.value[0]+1,
  //     implement:false
  //   })
  // },
  // 设置练习数量
  bindchange(e){
    this.setData({
      action_num: e.detail.current +1,
      implement:false
    })
  },
  bindinput(e){
    console.log(e)
      this.setData({
        action_num: e.detail.value,
      })
  },
  // 运动重量减少
  reduce:function(){
      if(this.data.record_weight>=0.5){
        this.setData({
          record_weight:this.data.record_weight-0.5
        })
      }
  },
  // 运动重量增加
  add:function(){
    this.setData({
      record_weight:this.data.record_weight+0.5
    })
  },
  // 下一个动作
  xTonext:function(){
    let that =this;
    if(getApp().globalData.lock==true){
      return
    }
    
    if(that.data.info.is_record_weight==true && that.data.info.record.length==0 && that.data.record_weight==0){
      wx.showModal({
        title: "提示",
        content: "请输入你负重物品的大概重量",
        showCancel: false,
        confirmText:'好的'
      })
      return
    }
    if(that.data.action_num ==''){
      if(that.data.info.record_mode==1){
        wx.showModal({
          title: "提示",
          content: "请输入练习时长",
          showCancel: false,
          confirmText:'好的'
        })
        return
      }else{
        wx.showModal({
          title: "提示",
          content: "请滑动选择练习数量",
          showCancel: false,
          confirmText:'好的'
        })
        return
      }
      
    }
    getApp().globalData.lock=true
    wx.showLoading({
      title: '保存中',
    })
    let _dataObj = {
      week: that.data.week,
      treatment_item_id: that.data.treatment_item_id,//任务_id
      action_id:that.data.action_id,// 动作_id
      record_weight:that.data.record_weight
    }
    if(that.data.info.record_mode==2){
      _dataObj.movements_number=that.data.action_num
    }else if(that.data.info.record_mode==1){
      _dataObj.exercise_duration=that.data.action_num
    }
    wx.cloud.callFunction({
      name: 'digital_therapy',
      data: {
        action: "add_action_practice_record",
        dataObj: _dataObj
      }
    }).then(res => {
      wx.hideLoading()
      getApp().globalData.lock=false
      wx.navigateBack({
         delta:1
      })
      that.xChildReload()
          // that.next_action()
    }).catch(err=>{
      wx.hideLoading()
      getApp().globalData.lock=false
    })
  },
  xTocomplete:function(){
    let that =this;
    if(getApp().globalData.lock==true){
      return
    }
    if(that.data.info.is_record_weight==true && that.data.info.record.length==0 && that.data.record_weight==0){
      wx.showModal({
        title: "提示",
        content: "请输入你负重物品的大概重量",
        showCancel: false,
        confirmText:'好的'
      })
      return
    }
    if(that.data.action_num ==''){
      if(that.data.info.record_mode==1){
        wx.showModal({
          title: "提示",
          content: "请输入练习时长",
          showCancel: false,
          confirmText:'好的'
        })
        return
      }else{
        wx.showModal({
          title: "提示",
          content: "请滑动选择练习数量",
          showCancel: false,
          confirmText:'好的'
        })
        return
      }
      
    }
    getApp().globalData.lock=true
    wx.showLoading({
      title: '保存中',
    })
    let _dataObj = {
      week: that.data.week,
      treatment_item_id: that.data.treatment_item_id,//任务_id
      action_id:that.data.action_id,// 动作_id
      record_weight:that.data.record_weight,
      practice_feeling:that.data.practice_feeling,
      is_last_action:true
    }
    if(that.data.info.record_mode==2){
      _dataObj.movements_number=that.data.action_num
    }else if(that.data.info.record_mode==1){
      _dataObj.exercise_duration=that.data.action_num
    }
    
    wx.cloud.callFunction({
      name: 'digital_therapy',
      data: {
        action: "add_action_practice_record",
        dataObj: _dataObj
      }
    }).then(res => {
      wx.hideLoading()
      getApp().globalData.lock=false
      wx.navigateBack({
        delta:1
      })
      that.xChildReload()
    }).catch(err=>{
      wx.hideLoading()
      getApp().globalData.lock=false
    })
  },
  // 无法执行此动作
  xTonextundo:function(){
    let that =this;
    if(that.data.retroaction==''){
      return
    }
    if(getApp().globalData.lock==true){
      return
    }
    getApp().globalData.lock=true
    wx.showLoading({
      title: '保存中',
    })
    let _dataObj = {
      week: that.data.week,
      treatment_item_id: that.data.treatment_item_id,//任务_id
      action_id:that.data.action_id,// 动作_id
      record_weight:0,
      retroaction:that.data.retroaction
    }
    if(that.data.info.record_mode==2){
      _dataObj.movements_number=0
    }else if(that.data.info.record_mode==1){
      _dataObj.exercise_duration=0
    }
    wx.cloud.callFunction({
      name: 'digital_therapy',
      data: {
        action: "add_action_practice_record",
        dataObj: _dataObj
      }
    }).then(res => {
          wx.hideLoading()
          getApp().globalData.lock=false
          wx.navigateBack({
            delta:1
          })
          // that.next_action()
    }).catch(err=>{
      wx.hideLoading()
      getApp().globalData.lock=false
    })
  },
  next_action:function(){
    let that =this;
    if(that.data.next._id){
      wx.redirectTo({
        url: '/subtool/pages/action/details/index?week='+that.data.week+'&action_id='+ that.data.next._id+'&treatment_item_id='+that.data.treatment_item_id,
      })
      wx.setNavigationBarTitle({
        title: that.data.next.name,
      })
    }else{
      wx.navigateBack({
        delta:1
      })
    }
  },
  previous_action:function(){
    let that =this;
    wx.redirectTo({
      url: '/subtool/pages/action/details/index?week='+that.data.week+'&action_id='+ that.data.previous._id+'&treatment_item_id='+that.data.treatment_item_id,
    })
    wx.setNavigationBarTitle({
      title: that.data.previous.name,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})